//
//  HTopView.m
//  QuanJieShopProject
//
//  Created by 刘中义 on 2018/2/25.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "HTopView.h"

@interface HTopView()
@property (nonatomic, weak) UIView *lineView;//按钮横线view
@property (nonatomic, strong) NSMutableArray *spaceBtnArray;
@property (nonatomic, assign) CGFloat btnWidth;
@property (nonatomic, assign) NSInteger nowBtnTag;//当前选中的按钮tag
@end

@implementation HTopView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.frame = frame;
    }
    return self;
}

- (void)setUI {
    //添加按钮
    NSMutableArray *spaceBtnArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.btnArray.count; i++) {
        
        UIButton *btn = [[UIButton alloc] init];
        btn.tag = i + self.btnTagOne;
        CGFloat btnW = UISCREEN_WIDTH / self.btnArray.count;
        self.btnWidth = btnW;
        btn.frame = CGRectMake(i*btnW, 0, btnW, self.frame.size.height);
        
        
        // 设置坐标
        [spaceBtnArray addObject:btn];
        self.spaceBtnArray = spaceBtnArray;
        [self addSubview:btn];
        
        
        //设置按钮字体大小和字体颜色
        [btn setTitleColor:self.btnTitleClolor forState:UIControlStateNormal];
        [btn setTitleColor:self.btnSelectClolor forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {//第一次运行默认选中第一个按钮
            //横线view
            UIView *lineView = [[UIView alloc] init];
            self.lineView = lineView;
            self.lineView.frame = CGRectMake(10, self.frame.size.height, btnW - 20, 2);
            self.lineView.layer.cornerRadius = 1.5;
            self.lineView.clipsToBounds = YES;
            self.lineView.layer.masksToBounds = YES;
            self.lineView.backgroundColor = self.lineColor;
            [self addSubview:lineView];
            [self btnClick:btn];
        }
    }
    
    //给按钮赋值
    for (int k = 0;k < self.btnArray.count;k++) {
        id btnType = self.btnArray[k];
        UIButton *nowBtn = spaceBtnArray[k];
        if ([btnType isKindOfClass:[NSString class]]) { //是字符串
            NSString *textBtn = btnType;
            
            [nowBtn setTitle:textBtn forState:UIControlStateNormal];
            
            [nowBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }else if ([btnType isKindOfClass:[NSArray class]]) { //是数组就是图片
            NSArray *imageArray = btnType;
            NSString *imageBtnName = [NSString stringWithFormat:@"%@",imageArray[0]];
            NSString *imageSelectName = [NSString stringWithFormat:@"%@",imageArray[1]];
            [nowBtn setImage:[UIImage imageNamed:imageBtnName] forState:UIControlStateNormal];
            [nowBtn setImage:[UIImage imageNamed:imageSelectName] forState:UIControlStateSelected];
        }
    }
    
    
}

- (void)setGesture:(UIView *)superView{
    
    UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    
    // 轻扫的方向
    swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [superView addGestureRecognizer:swipeGestureRecognizer];
    
    UISwipeGestureRecognizer *swipeGestureRecognizerleft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    // 轻扫的方向
    swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [superView addGestureRecognizer:swipeGestureRecognizerleft];
}

//  轻扫手势的方法
- (void)swipeGestureRecognizer:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
    /*
     UISwipeGestureRecognizerDirectionRight = 1 << 0,
     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
     UISwipeGestureRecognizerDirectionUp    = 1 << 2,
     UISwipeGestureRecognizerDirectionDown  = 1 << 3
     */
    if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        if (self.nowBtnTag != 0 + self.btnTagOne) {
            UIButton *btn = self.spaceBtnArray[self.nowBtnTag - 1 - self.btnTagOne];
            [self btnClick:btn];
        }
        
    }
    if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        if (self.nowBtnTag != self.spaceBtnArray.count -1 + self.btnTagOne) {
            UIButton *btn = self.spaceBtnArray[self.nowBtnTag + 1 - self.btnTagOne];
            [self btnClick:btn];
        }
        
    }
}

- (void)btnClick:(UIButton *)btnSelect {
    
    if ([self.delegate respondsToSelector:@selector(HTopViewBtnClick:)]) {
        [self.delegate HTopViewBtnClick:btnSelect.tag];
    }
    for (int i = 0; i < self.spaceBtnArray.count;i++) {
        UIButton *btn = self.spaceBtnArray[i];
        btn.selected = NO;
        if (i+self.btnTagOne == btnSelect.tag) {
            btnSelect.selected = YES;
            self.nowBtnTag = btnSelect.tag;
            [UIView animateWithDuration:0.25 animations:^{
                self.lineView.transform =  CGAffineTransformMakeTranslation(i * self.btnWidth, 0);//xy移动距离;
            }];
        }
    }
    
}

- (void)tagBtnClick:(NSInteger)tag {
    
    UIButton *btn = self.spaceBtnArray[tag];
    [self btnClick:btn];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
